package com.springboot.demo.controller;

import java.io.File;

import javax.mail.internet.MimeMessage;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.core.io.FileSystemResource;
import org.springframework.mail.MailException;
import org.springframework.mail.SimpleMailMessage;
import org.springframework.mail.javamail.JavaMailSender;
import org.springframework.mail.javamail.MimeMessageHelper;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import org.thymeleaf.TemplateEngine;
import org.thymeleaf.context.Context;

import javax.mail.MessagingException;


/**
 * @author peter
 * @title: EmailController
 * @projectName springAll-peter
 * @description: 邮件
 * @date 19-9-18上午11:47
 */

@Controller
@RequestMapping("/email")
public class EmailController {


    @Autowired
    private JavaMailSender jms;

    @Autowired
    private TemplateEngine templateEngine;

    @Value("${spring.mail.username}")
    private String from;

    @RequestMapping("/sendSimpleEmail")
    public String sendSimpleEmail() {
        try {
            SimpleMailMessage mailMessage = new SimpleMailMessage();
            mailMessage.setFrom(from);
            mailMessage.setTo("827358889@qq.com");//接受地址

            mailMessage.setText("使用Springboot 发送简单邮件");
            jms.send(mailMessage);
            return "发送成功";
        } catch (MailException e) {
            e.printStackTrace();
            return e.getMessage();
        }
    }


    @RequestMapping("/sendHtmlEmail")
    public String sendHtmlEmail() {
        MimeMessage mimeMessage = null;
        try {

            mimeMessage = jms.createMimeMessage();
            MimeMessageHelper helper = new MimeMessageHelper(mimeMessage, true);


            helper.setFrom(from);
            helper.setTo("827358889@qq.com");//接收地址
            helper.setSubject("一封HTML格式的邮件");//标题
            //带HTML格式的内容
            StringBuffer sb = new StringBuffer("<p style='color:#42b983'>使用Spring boot 发送HTML格式邮件。</p>");
            helper.setText(sb.toString(), true);
            jms.send(mimeMessage);
            return "发送成功";
        } catch (MailException | MessagingException e) {
            e.printStackTrace();
            return e.getMessage();
        }
    }


    @RequestMapping("/sendAttachmentEmail")
    public String sendAttachmentEmail() {
        MimeMessage mimeMessage = null;
        try {

            mimeMessage = jms.createMimeMessage();
            MimeMessageHelper helper = new MimeMessageHelper(mimeMessage, true);


            helper.setFrom(from);
            helper.setTo("827358889@qq.com");//接收地址
            helper.setSubject("一封带附件的邮件");//标题
            helper.setText("详情参见附件的内容！");//内容

            //传入附件
            FileSystemResource file = new FileSystemResource(new File("/media/peter/Data/dev/workspace/springAll-peter/22.Spring-Boot-Email/src/main/resources/static/file/风景.jpg"));
            helper.addAttachment("风景.jpg", file);
            jms.send(mimeMessage);

            return "发送成功";
        } catch (MailException | MessagingException e) {
            e.printStackTrace();
            return e.getMessage();
        }
    }


    @RequestMapping("/sendInlineMail")
    public String sendInlineMail() {
        MimeMessage mimeMessage = null;
        try {

            mimeMessage = jms.createMimeMessage();
            MimeMessageHelper helper = new MimeMessageHelper(mimeMessage, true);


            helper.setFrom(from);
            helper.setTo("827358889@qq.com");//接收地址
            helper.setSubject("一封带附件的邮件");//标题
            helper.setText("<html><body>博客图：<img src='cid:img'></body></html>", true);//内容

            //传入附件
            FileSystemResource file = new FileSystemResource(new File("/media/peter/Data/dev/workspace/springAll-peter/22.Spring-Boot-Email/src/main/resources/static/file/风景.jpg"));
            helper.addInline("img", file);
            jms.send(mimeMessage);

            return "发送成功";
        } catch (MailException | MessagingException e) {
            e.printStackTrace();
            return e.getMessage();
        }
    }

    @RequestMapping("/sendTemplateEmail")
    public String sendTemplateEmail(String code) {
        MimeMessage mimeMessage = null;
        try {

            mimeMessage = jms.createMimeMessage();
            MimeMessageHelper helper = new MimeMessageHelper(mimeMessage, true);


            helper.setFrom(from);
            helper.setTo("827358889@qq.com");//接收地址
            helper.setSubject("邮件模板测试");//标题
            //处理邮件模板
            Context context = new Context();
            context.setVariable("code", code);
            String emailTemplate = templateEngine.process("emailTemplate", context);
            helper.setText(emailTemplate, true);

            jms.send(mimeMessage);

            return "发送成功";
        } catch (MailException | MessagingException e) {
            e.printStackTrace();
            return e.getMessage();
        }
    }


    @RequestMapping("/sendTemplateEmail1")
    public String sendTemplateEmail(Model model) {

        String code = "tiger.LTD.co";
        String orderId = "000100020001";

        model.addAttribute("code", code);
        model.addAttribute("orderId", orderId);


        return "orderTemplate";
    }


}
